package com.fingard.dsp.bank.directbank.zjrc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ZJRC01Balance extends ZJRC01Base implements IBalance {

    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            tranCode = "100001";
            serialNo = p_req7506.reqHeader.transSeqID;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("iAccAll");
            int count = 0;
            while (p_req7506.readRecord()) {
                count++;
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("currencyType", convertCurToBank(p_req7506.readedDetail.actCur));
                //先默认为实际账户吧
                xmlWriter.writeElementString("accountType", "1");//账户类型  1:实账户5:虚账户
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            //模拟银行返回
            /*tmpStrRet[0] = "";
            tmpStrRet[1] ="<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                    "<ebank>\n" +
                    "\t<resHead>\n" +
                    "\t\t<retCode>0000</retCode>\n" +
                    "\t\t<errMsg>返回错误信息</errMsg>\n" +
                    "\t\t<serialNo>12345678</serialNo>\n" +
                    "\t</resHead>\n" +
                    "\t<resBody>\n" +
                    "\t\t<accList>\n" +
                    "\t\t\t<row>\n" +
                    "\t\t\t\t<accNo>123456</accNo>\n" +
                    "\t\t\t\t<accName>nihao</accName>\n" +
                    "\t\t\t\t<accKind>0011</accKind>\n" +
                    "\t\t\t\t<accStatus>1</accStatus>\n" +
                    "\t\t\t\t<lostStatus>1</lostStatus>\n" +
                    "\t\t\t\t<belongBranch></belongBranch>\n" +
                    "\t\t\t\t<feePayType>1</feePayType>\n" +
                    "\t\t\t\t<openDate>1</openDate>\n" +
                    "\t\t\t\t<amount>23.99</amount>\n" +
                    "\t\t\t\t<balanceAvailable>20.78</balanceAvailable>\n" +
                    "\t\t\t\t<success></success>\n" +
                    "\t\t\t\t<virturlAccNo></virturlAccNo>\n" +
                    "\t\t\t</row>\n" +
                    "\t\t</accList>\n" +
                    "\t</resBody>\n" +
                    "</ebank>\n";*/
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                int index = tmpStrRet[1].indexOf("</ebank>");
                tmpStrRet[1] = tmpStrRet[1].substring(0,index+8);
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/ebank/resHead/retCode");
                    String tmpStatusText = tmpBkReader.getFirstText("/ebank/resHead/errMsg");
                    if (tmpStatus != null && isActiveRetCode(tmpStatus)) {
                        while (tmpBkReader.read("/ebank/resBody/accList/row")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("accNo");
                            if (!tmpBkReader.readedXElement.getSingleText("accStatus").equals("2")) {//accStatus 1-正常、2-销户、3-新开户（未启用单位户有效）
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("accNo");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balanceAvailable");
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("amount");
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "账户已销户";
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStatusText);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

}
